package com.uniledger.exchange.stellar;

import com.uniledger.exchange.BuildConfig;
import com.uniledger.exchange.model.OkSingleton;

import org.stellar.sdk.AssetTypeCreditAlphaNum4;
import org.stellar.sdk.KeyPair;
import org.stellar.sdk.Memo;
import org.stellar.sdk.Network;
import org.stellar.sdk.PaymentOperation;
import org.stellar.sdk.Server;
import org.stellar.sdk.Transaction;
import org.stellar.sdk.responses.AccountResponse;

import java.io.IOException;

import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.FlowableEmitter;
import io.reactivex.FlowableOnSubscribe;

public class TransactionUtils {

    public static final String ACTION_TRUST="com.uniledger.exchange.stellar.TrustIntentService";

    public static Flowable<String> transaction(String amount, String memo, String accountId) {
        return Flowable.create(new FlowableOnSubscribe<String>() {
            @Override
            public void subscribe(FlowableEmitter<String> emitter) throws Exception {
                try {
                    KeyPair source = OkSingleton.getInstance().getSourceKeyPair();
                    KeyPair issuing = OkSingleton.getInstance().getIssuingKeyPair();
                    KeyPair destination = KeyPair.fromAccountId(accountId);
                    Network.use(new Network("Standalone Network ; February 2017"));
                    Server server = new Server(BuildConfig.STELLAR_HOST);
                    server.accounts().account(destination);
                    AccountResponse sourceAccount = server.accounts().account(source);
                    Transaction transaction = new Transaction.Builder(sourceAccount)
                            .addOperation(new PaymentOperation.Builder(destination, new AssetTypeCreditAlphaNum4("AED", issuing), amount).build())
                            .addMemo(Memo.text("先心安"))
                            .build();
                    transaction.sign(source);
                    emitter.onNext(transaction.toEnvelopeXdrBase64());
                    emitter.onComplete();
                } catch (IOException e) {
                    emitter.onError(e);
                } catch (Exception e) {
                    emitter.onError(e);
                }
            }
        }, BackpressureStrategy.BUFFER);
    }

    public static Flowable<String> transaction(String amount, String memo) {
        return Flowable.create(new FlowableOnSubscribe<String>() {
            @Override
            public void subscribe(FlowableEmitter<String> emitter) throws Exception {
                try {
                    KeyPair source = OkSingleton.getInstance().getSourceKeyPair();
                    KeyPair issuing = OkSingleton.getInstance().getIssuingKeyPair();
                    KeyPair destination = OkSingleton.getInstance().getBaseKeyPair();
                    Network.use(new Network("Standalone Network ; February 2017"));
                    Server server = new Server(BuildConfig.STELLAR_HOST);
                    server.accounts().account(destination);
                    AccountResponse sourceAccount = server.accounts().account(source);
                    Transaction transaction = new Transaction.Builder(sourceAccount)
                            .addOperation(new PaymentOperation.Builder(destination, new AssetTypeCreditAlphaNum4("AED", issuing), amount).build())
                            .addMemo(Memo.text(memo))
                            .build();
                    transaction.sign(source);
                    emitter.onNext(transaction.toEnvelopeXdrBase64());
                    emitter.onComplete();
                } catch (IOException e) {
                    emitter.onError(e);
                } catch (Exception e) {
                    emitter.onError(e);
                }
            }
        }, BackpressureStrategy.BUFFER);
    }
}
